﻿<sdk:Page x:Class="SampleBrowser.Views.SampleWatermark" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
           xmlns:doc="http://schemas.firstfloorsoftware.com/documenttoolkit"
           xmlns:sample="http://schemas.firstfloorsoftware.com/documenttoolkit/samplebrowser"
           xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Watermark">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid.Resources>
            <ControlTemplate x:Key="WatermarkedFixedPageViewerTemplate" TargetType="doc:FixedPageViewer">
                <Grid x:Name="LayoutRoot">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="PageStates">
                            <VisualState x:Name="PageLoading" />
                            <VisualState x:Name="PageLoaded">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PagePresenter" Storyboard.TargetProperty="Visibility" Duration="0">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="PageError">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageErrorElement" Storyboard.TargetProperty="Visibility" Duration="0">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentPresenter x:Name="PagePresenter" Content="{TemplateBinding FixedPage}" Visibility="Collapsed" />
                    <Grid x:Name="PageErrorElement" DataContext="{TemplateBinding FixedPageError}" Visibility="Collapsed" Margin="10" HorizontalAlignment="Left">
                        <TextBlock Foreground="Red" TextWrapping="Wrap">
                                <Bold>Failed to render page</Bold>
                                <LineBreak />
                                <Run Text="{Binding FixedPageError}" />
                        </TextBlock>
                    </Grid>
                    <!-- watermark -->
                    <Grid Width="{Binding FixedPageSize.Width, RelativeSource={RelativeSource TemplatedParent}}" Height="{Binding FixedPageSize.Height, RelativeSource={RelativeSource TemplatedParent}}">
                        <!-- auto-adjust clip -->
                        <i:Interaction.Behaviors>
                            <sample:ClipToBoundsBehavior />
                        </i:Interaction.Behaviors>
                        <Border Background="{StaticResource HighlightBrush}" Opacity=".8" HorizontalAlignment="Right" VerticalAlignment="Top" RenderTransform="1,1,-1,1,140,-140">
                            <TextBlock Text="WATERMARK" FontWeight="Bold" FontSize="16" Foreground="White" Margin="128,12,128,12" />
                        </Border>
                    </Grid>
                </Grid>
            </ControlTemplate>
            <ControlTemplate x:Key="WatermarkedDocumentViewerTemplate" TargetType="doc:DocumentViewer">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <ScrollViewer x:Name="ScrollViewer" Background="Transparent" BorderThickness="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                        <ItemsControl x:Name="Pages">
                            <i:Interaction.Behaviors>
                                <doc:PageVirtualizer />
                            </i:Interaction.Behaviors>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid Width="{Binding DesiredPageContainerSize.Width}"
                                          Height="{Binding DesiredPageContainerSize.Height}">
                                        <Border Background="White" BorderBrush="{StaticResource SliderTrackBrush}" BorderThickness="1" Margin="4" HorizontalAlignment="Center" VerticalAlignment="Center">
                                            <doc:FixedPageViewer FixedPage="{Binding FixedPage}" 
                                                                 FixedPageSize="{Binding FixedPageSize}"
                                                                 FixedPageError="{Binding FixedPageError}"
                                                                 Scale="{Binding DesiredPageScale}"
                                                                 Template="{StaticResource WatermarkedFixedPageViewerTemplate}">
                                                <i:Interaction.Behaviors>
                                                    <doc:NavigationBehavior TextContainer="{Binding TextContainer}" />
                                                </i:Interaction.Behaviors>
                                            </doc:FixedPageViewer>
                                        </Border>
                                    </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Grid.Resources>

        <TextBlock x:Name="HeaderText" Style="{StaticResource HeaderTextStyle}" Text="Watermark"/>

        <doc:DocumentDataSource x:Name="DataSource" LoadError="DataSource_LoadError" />

        <doc:DocumentViewer x:Name="Viewer"
                            Grid.Row="1"
                            Template="{StaticResource WatermarkedDocumentViewerTemplate}"
                            DocumentDataSource="{Binding ElementName=DataSource}"
                            ViewMode="{Binding SelectedViewMode, ElementName=ViewModePicker}">
        </doc:DocumentViewer>

        <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="0,8,0,0" HorizontalAlignment="Center">
            <doc:HistoryNavigator History="{Binding History, ElementName=Viewer}" />
            <doc:PageNavigator x:Name="Navigator"
                               PageCount="{Binding PageCount, ElementName=Viewer}"
                               PageIndex="{Binding PageIndex, ElementName=Viewer, Mode=TwoWay}"
                               Margin="4,0,0,0" />
            <doc:ViewModePicker x:Name="ViewModePicker" Margin="4,0,0,0" />

            <Button Margin="4,0,0,0" Padding="7">
                <Button.Command>
                    <sample:OpenPdfOrXpsFromDiskCommand DocumentDataSource="{Binding ElementName=DataSource}" />
                </Button.Command>
                <Button.Content>
                    <Image Source="/SampleBrowser;component/Assets/Icons/open.png" />
                </Button.Content>
            </Button>

        </StackPanel>
    </Grid>
</sdk:Page>
